﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.BLL.Core.IService;
using Empires.CRL.DataContract;
using Empires.CRL.Tools;
using Empires.BLL.ServerProvider;

namespace Empires.BLL.Core.Implement
{
    public class CompanyBlogService : ICompanyBlogService
    {
        public List<DefaultDataPack> GetBlogList(string copName, int page, int size)
        {
            RAL.Access.ICompanyBlogAccess access = new RAL.Pump.CompanyBlogDataPump();
            int itemCount = access.GetBlogItemCount(copName);
            return CompanyHelper.InjuectPagingInfo((currentPage) =>
            {
                return access.GetBlogList(copName, currentPage, size);
            },
            itemCount, page, size);

            //int pagecount = itemCount % size == 0 ? itemCount / size : itemCount / size + 1;
            //page = page < 0 ? 0 : page;
            //page = page > pagecount - 1 ? pagecount - 1 : page;
            //int previouspage = page == 0 ? 0 : page - 1;
            //int nextpage = page == pagecount - 1 ? pagecount - 1 : page + 1;
            //var list = access.GetBlogList(copName, page, size);

            //return list.Select(c =>
            //{
            //    c.ABSTRACT = c.CONTENT.GetStringWithoutHTMLLabel().GetStringSegment(0, 300) + ". . . . . .";
            //    c.ITEMCOUNT = itemCount;
            //    c.PAGESIZE = size;
            //    c.CURRENTPAGE = page + 1;
            //    c.PAGECOUNT = pagecount;
            //    c.PREVIOUSPAGE = previouspage;
            //    c.NEXTPAGE = nextpage;
            //    c.FIRSTPAGE = 0;
            //    c.LASTPAGE = pagecount - 1;
            //    return c;
            //}).ToList();
        }

        public List<DefaultDataPack> GetBlogArticle(string copName, int articleId)
        {
            RAL.Access.ICompanyBlogAccess access = new RAL.Pump.CompanyBlogDataPump();
            return access.GetBlogArticle(copName, articleId);
        }


        public int DeleteBlogArticle(string copName, int id)
        {
            RAL.Access.ICompanyBlogAccess access = new RAL.Pump.CompanyBlogDataPump();
            return access.DeleteBlogArticle(copName, id);
        }


        public bool UpdateBlogArticle(string copname, BlogDataPack blogDataPack)
        {
            if (!String.IsNullOrEmpty(blogDataPack.MainPhoto))
            {
                var result = ServerProvider.Tools.UploadFiles(
                    new List<UploadFileDataPack>() { 
                        new UploadFileDataPack(){
                            BasePath = String.Format(Empires.CRL.Tools.Utils.ReadAppSetting("CustomerImage"),copname),
                            TypeFilter= new List<FileType>(){ FileType.jpg,FileType.png },
                            UploadName="MainPhoto",
                            CopyMode= CopyMode.rename
                        }
                    }, System.Web.HttpContext.Current.Request.Files);
                string url = string.Empty;
                if (result.TryGetValue("MainPhoto", out url))
                {
                    blogDataPack.MainPhoto = String.Format("<img src=\"{0}\" />", url);
                }
                else
                {
                    blogDataPack.MainPhoto = String.Empty;
                }
            }

            RAL.Access.ICompanyBlogAccess access = new RAL.Pump.CompanyBlogDataPump();
            return access.UpdateBlogArticle(copname, blogDataPack);
        }


        public bool AddBlogArticle(string copname, BlogDataPack blogDataPack)
        {
            if (!String.IsNullOrEmpty(blogDataPack.MainPhoto))
            {
                var result = ServerProvider.Tools.UploadFiles(
                    new List<UploadFileDataPack>() { 
                        new UploadFileDataPack(){
                            BasePath = String.Format(Empires.CRL.Tools.Utils.ReadAppSetting("CustomerImage"),copname),
                            TypeFilter= new List<FileType>(){ FileType.jpg,FileType.png },
                            UploadName="MainPhoto",
                            CopyMode= CopyMode.rename
                        }
                    }, System.Web.HttpContext.Current.Request.Files);
                string url = string.Empty;
                if (result.TryGetValue("MainPhoto", out url))
                {
                    blogDataPack.MainPhoto = String.Format("<img src=\"{0}\" />", url);
                }
                else
                {
                    blogDataPack.MainPhoto = String.Empty;
                }
            }

            RAL.Access.ICompanyBlogAccess access = new RAL.Pump.CompanyBlogDataPump();
            return access.AddBlogArticle(copname, blogDataPack);
        }
    }
}
